package com.cneiu.ppms.web.validator;

import java.util.Enumeration;
import java.util.HashMap;

import com.cneiu.ppms.utils.StringUtils;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

/**
 * 登录验证器数据验证器
 * @author jiangli
 * @date 2013-04-16
 */
public class LoginValidator extends Validator {

	@Override
	protected void validate(Controller controller) {
		validateRequiredString("account", "account", "用户名不能为空!");
		validateRequiredString("password", "password", "密码不能为空!");
		validateRequiredString("verify", "verify", "验证码不能为空!");
		validateRequiredString("callback", "callback", "登录请求参数错误!");
		String verify = controller.getPara("verify");
		String rand = controller.getSessionAttr("rand");
		controller.removeSessionAttr("rand");
		validateEqualString(verify, rand, "rand", "验证码输入错误!");
	}

	@Override
	protected void handleError(Controller controller) {
		String actionKey = getActionKey();
		HashMap<String, Object> map = new HashMap<String, Object>();
		Enumeration<String> strMal = controller.getAttrNames();
		while(strMal.hasMoreElements()){
			String key = strMal.nextElement();
			map.put(key, controller.getAttr(key));
		}
		String callback = controller.getPara("callback");
		if (actionKey.equals("/users/user/login")) {
			String Json = StringUtils.renderAjax(callback, false,map);
			controller.renderText(Json);
		}
	}
}